package com.rabbiter.oes.controller;

import com.rabbiter.oes.entity.Admin;
import com.rabbiter.oes.result.R;
import com.rabbiter.oes.service.impl.AdminServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "管理员相关接口")
@RestController
public class AdminController {

    @Autowired
    private AdminServiceImpl adminService;

    @ApiOperation(value = "查询全部管理员信息", notes = "查询全部管理员信息", httpMethod = "GET")
    @GetMapping("/admins")
    public R findAll() {
        return R.ok().data(adminService.findAll());
    }

    @ApiOperation(value = "根据id查询管理员信息", notes = "根据id查询管理员信息", httpMethod = "GET")
    @GetMapping("/admin/{id}")
    public R findById(@PathVariable("id") Integer id) {
        return R.ok().data(adminService.findById(id));
    }

    @ApiOperation(value = "根据id删除管理员信息", notes = "更具id删除管理员信息", httpMethod = "DELETE")
    @DeleteMapping("/admin/{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        adminService.deleteById(id);
        return R.ok();
    }

    @ApiOperation(value = "更新管理员信息", notes = "更新管理员信息", httpMethod = "PUT")
    @PutMapping("/admin")
    public R update(Admin admin) {
        return R.ok().data(adminService.update(admin));
    }

    @ApiOperation(value = "新增管理员信息", notes = "新增管理员信息", httpMethod = "POST")
    @PostMapping("/admin")
    public R add(Admin admin) {
        return R.ok().data(adminService.add(admin));
    }

    @ApiOperation(value = "重置管理员密码", notes = "重置管理员密码", httpMethod = "DELETE")
    @GetMapping("/admin/resetPsw/{id}/{oldPsw}/{newPsw}")
    public R resetPsw(@PathVariable("id") Integer id, @PathVariable("newPsw") String newPsw, @PathVariable("oldPsw") String oldPsw) {
        try {
            adminService.resetPsw(id, newPsw, oldPsw);
            return R.ok("修改密码成功！").data(true);
        }catch (Exception e) {
            e.printStackTrace();
            return R.ok(e.getMessage()).data(false);
        }
    }
}
